﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;

namespace eLuk.Entity
{
	[DataContract]
	public class IMAPCapability : eLukObject
	{
		
		public IMAPCapability( byte[] row )
		{
			//this.Raw = row;
		}

		public IMAPCapability( string commandResult )
		{
			//this.Raw = Encoding.UTF8.GetBytes( commandResult );
			//this.Encrypted = 
			Update( commandResult );
		}

		/// <summary>
		///     Gets whether the server supports the ACL extension
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc4314" />
		[DataMember]
		public bool Acl { get; set; }

		/// <summary>
		///     Contains a list of all capabilities supported by the server
		/// </summary>
		[DataMember]
		public string[] All { get; set; }

		/// <summary>
		///     A list of additional authentication mechanisms the server supports
		/// </summary>
		[DataMember]
		public string[] AuthenticationMechanisms { get; set; }

		/// <summary>
		///     Gets whether the server supports the BINARY extension
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc3516" />
		[DataMember]
		public bool Binary { get; set; }

		/// <summary>
		///     Gets whether the server supports the CATENATE extension
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc4469" />
		[DataMember]
		public bool Catenate { get; set; }

		/// <summary>
		///     Gets whether the server supports the CHILD extension
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc3348" />
		[DataMember]
		public bool Children { get; set; }

		/// <summary>
		///     Contains a list of compression mechanisms supported by the server
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc4978" />
		[DataMember]
		public string[] CompressionMechanisms { get; set; }

		/// <summary>
		///     Gets whether the server supports the CONDSTORE extension
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc4551" />
		[DataMember]
		public bool CondStore { get; set; }

		/// <summary>
		///     Contains a list of contexts supported by the server
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc5267" />
		[DataMember]
		public string[] Contexts { get; set; }

		/// <summary>
		///     Gets whether the server supports the CONVERT extension
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc5259" />
		[DataMember]
		public bool Convert { get; set; }

		/// <summary>
		///     Gets whether the server supports the CREATE-SPECIAL-USE extension
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc6154" />
		[DataMember]
		public bool CreateSpecialUse { get; set; }

		/// <summary>
		///     Gets whether the server supports the ENABLE extension
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc5161" />
		[DataMember]
		public bool Enable { get; set; }

		/// <summary>
		///     Gets whether the server supports the ESEARCH extension
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc4731" />
		[DataMember]
		public bool ESearch { get; set; }

		/// <summary>
		///     Gets whether the server supports the ESORT extension
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc5267" />
		[DataMember]
		public bool ESort { get; set; }

		/// <summary>
		///     Gets whether the server supports the FILTERS extension
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc5466" />
		[DataMember]
		public bool Filters { get; set; }

		/// <summary>
		///     Gets whether the server supports the ID extension
		/// </summary>
		[DataMember]
		public bool Id { get; set; }

		/// <summary>
		///     Gets whether the server supports the IDLE extension
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc2177" />
		[DataMember]
		public bool Idle { get; set; }

		/// <summary>
		///     Gets whether the LOGIN command is disabled on the server
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc2595" />
		[DataMember]
		public bool LoginDisabled { get; set; }

		/// <summary>
		///     Gets whether the METADATA extension is supported
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc5464" />
		[DataMember]
		public bool Metadata { get; set; }

		/// <summary>
		///     Gets whether the server supports the NAMESPACE extension
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc2342" />
		[DataMember]
		public bool Namespace { get; set; }

		/// <summary>
		///     Gets whether the server supports authentication through OAuth
		/// </summary>
		[DataMember]
		public bool XoAuth { get; set; }

		/// <summary>
		///     Gets whether the server supports authentication through OAuth2
		/// </summary>
		[DataMember]
		public bool XoAuth2 { get; set; }

		/// <summary>
		///     Gets whether the server supports the QUOTA extension
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc2087" />
		[DataMember]
		public bool Quota { get; set; }

		/// <summary>
		///     Gets whether the server supports the UNSELECT extension
		/// </summary>
		/// <see cref="http://tools.ietf.org/html/rfc3691" />
		[DataMember]
		public bool Unselect { get; set; }

		[DataMember]
		public bool XList { get; set; }

		[DataMember]
		public bool XGMExt1 { get; set; }

		internal void Update( string commandResult )
		{
			if ( string.IsNullOrEmpty( commandResult ) )
				return;

			commandResult = commandResult.Replace( "* CAPABILITY IMAP4rev1 ", "" );

			All = commandResult.Split( ' ' ).Where( _ => !string.IsNullOrEmpty( _.Trim() ) ).ToArray();

			AuthenticationMechanisms = All.Where( _ => _.StartsWith( "AUTH=" ) )
					.Select( _ => _.Substring( 5, _.Length - 5 ) ).ToArray();

			CompressionMechanisms = All.Where( _ => _.StartsWith( "COMPRESS=" ) )
					.Select( _ => _.Substring( 10, _.Length - 10 ) ).ToArray();

			Contexts = All.Where( _ => _.StartsWith( "CONTEXT=" ) )
					.Select( _ => _.Substring( 8, _.Length - 8 ) ).ToArray();

			foreach ( string s in All )
			{
				switch ( s )
				{
					case "X-GM-EXT-1":
						XGMExt1 = true;
						break;
					case "XLIST":
						XList = true;
						break;
					case "UNSELECT":
						Unselect = true;
						break;
					case "QUOTA":
						Quota = true;
						break;
					case "AUTH=XOAUTH2":
						XoAuth2 = true;
						break;
					case "AUTH=XOAUTH":
						XoAuth = true;
						break;
					case "NAMESPACE":
						Namespace = true;
						break;
					case "METADATA":
						Metadata = true;
						break;
					case "LOGINDISABLED":
						LoginDisabled = true;
						break;
					case "IDLE":
						Idle = true;
						break;
					case "ID":
						Id = true;
						break;
					case "FILTERS":
						Filters = true;
						break;
					case "ESORT":
						ESort = true;
						break;
					case "ESEARCH":
						ESearch = true;
						break;
					case "ENABLE":
						Enable = true;
						break;
					case "CREATE-SPECIAL-USE":
						CreateSpecialUse = true;
						break;
					case "CONVERT":
						Convert = true;
						break;
					case "CONDSTORE":
						CondStore = true;
						break;
					case "CHILDREN":
						Children = true;
						break;
					case "CATENATE":
						Catenate = true;
						break;
					case "BINARY":
						Binary = true;
						break;
					case "ACL":
						Acl = true;
						break;
				}
			}
		}
	}
}
